from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response
from .models import *
from .serializers import PecificationSer,SpecificationOptionSer,SKUSpecificationSer
from django.core.paginator import Paginator
from goods.models import SPU
from goods.serializers import SpuSer
# Create your views here.


# 获取所有的规格数据
class SpuSpecView(APIView):
    def get(self,request):
        page = request.query_params.get('page')
        pagesize = request.query_params.get('pagesize')
        pecofocaton = SPUSpecification.objects.all()
        count = pecofocaton.count()

        pag = Paginator(pecofocaton,pagesize)

        dada = pag.page(page)

        ser = PecificationSer(dada,many=True)

        return Response({
            'results':ser.data,
            'count':count
        })

    # 添加
    def post(self,request):
        # 接受前端数据
        name = request.data.get('name')
        spu = request.data.get('spu')

        # 判断是否接受
        if not all([name,spu]):
            return Response({
                'code':400,
                'msg':'接受的数据为空'
            })

        try:
            SPUSpecification.objects.create(
                name=name,
                spu_id=spu,
            )
            return Response({
                'code':201,
                'msg':'添加成功'
            })
        except Exception as e:
            print('添加失败信息',e)
            return Response({
                'code':400,
                'msg':'添加失败'
            })




# 删除和添加规格
class SpuSpecs(APIView):
    # 查看
    def get(self,request,id):
        try:
            spu = SPUSpecification.objects.get(id=id)
        except Exception as e:
            return Response({
                'code':400,
                'msg':'错误信息'
            })

        ser = PecificationSer(spu)
        return Response(ser.data)
    # 修改
    def put(self,request ,id):
        # 接受前端数据
        name = request.data.get('name')
        spu = request.data.get('spu')

        print('前端数据',spu,name)

        if not all([name,spu]):
            return Response({
                'code':400,
                'msg':'没有接受到数据'
            })
        try:
            spur = SPUSpecification.objects.get(id=id)
            print('dasd阿萨德',spur)
        except Exception as e:
            print('错误信息',e)
            return Response({
                'code':400,
                'msg':'没有此信息'
            })



        try:
            spur.name = name
            spur.spu_id = spu
            spur.save()
            return Response({
                'code':200,
                'msg':'修改成功'
            })
        except Exception as e:
            try:
                name_spu = SPU.objects.get(name=spu)
            except Exception as e:
                return Response({
                    'code': 400,
                    'msg': '没有找到数据'
                })
            name_id = name_spu.id

            spur.name = name
            spur.spu_id = name_id
            spur.save()
            return Response({
                'code': 200,
                'msg': '修改成功'
            })



    def delete(self,request,id):
        # 删除
        try:
            SPUSpecification.objects.get(id=id).delete()
            return Response({
                'code':204,
                'msg':'删除成功'
            })
        except Exception as e:
            print('删除错误',e)
            return Response({
                'code':400,
                'msg':'删除错误'
            })



# 规格选项添加和展示

class OptionsView(APIView):
    def get(self,request):
        page = request.query_params.get('page')
        pagesize = request.query_params.get('pagesize')
        print('小达达',page,pagesize)
        spec = SpecificationOption.objects.all()
        count = spec.count()

        pag = Paginator(spec,pagesize)
        dada = pag.page(page)
        ser = SpecificationOptionSer(dada,many=True)

        return Response({
            'code':200,
            'msg':'获取成功',
            'data':ser.data,
            'count':count
        })

    def post(self,request):
        # 接受收前端数据
        name = request.data.get('name')
        specs_id = request.data.get('specs_id')

        if not all([name,specs_id]):
            return Response({
                'code':500,
                'msg':'没有接受到前端数据'
            })
        try:
            SpecificationOption.objects.create(
                value=name,
                spec_id=specs_id
            )
            return Response({
                'code':200,
                'msg':'添加成功',
            })
        except Exception as e:
            print('添加失败',e)
            return Response({
                'code':500,
                'msg':'添加失败'
            })



# 规格选项编辑和删除

class OptionsbianView(APIView):
    def get(self,request,id):
        try:
            spe = SpecificationOption.objects.get(id=id)
        except Exception as e:
            return Response({
                'code':400,
                'msg':'没有这条信息'
            })

        ser = SpecificationOptionSer(spe)
        return Response(ser.data)

    # 编辑
    def put(self,request,id):
        # 接受前端信息
        name = request.data.get('name')
        spec_id = request.data.get('spec_id')

        print('登达',name,spec_id)

        if not all([name,spec_id]):
            return Response({
                'code':400,
                'msg':'没有接受到前端信息'
            })

        try:
            spe = SpecificationOption.objects.get(id=id)
        except Exception as e:
            print('错误修改后',e)
            return Response({
                'code':400,
                'msg':'没有这条信息'
            })

        try:
            spe.value = name
            spe.spec_id = spec_id
            spe.save()

            return Response({
                'code':200,
                'msg':'修改成功'
            })
        except Exception as e:
            print('修改失败',e)
            return Response({
                'code':500,
                'msg':'修改失败'
            })

    # 删除
    def delete(self,request,id):
        try:
            SpecificationOption.objects.get(id=id).delete()
            return Response({
                'code':204,
                'msg':'删除成功'
            })
        except Exception as e:
            print('删除失败',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })



from utils.QNYtoken import token_name
# 获取图片token
class GetTokenView(APIView):
    def get(self,request):
        token = token_name()
        return Response({
            'code':200,
            'msg':'token发送成功',
            'token':token
        })


# 添加图片和获取图片

class SkuImgeView(APIView):
    def get(self,request):
        skuimage = SKUImage.objects.all()
        sku_list = []
        for i in skuimage:
            sku_list.append({
                'id':i.id,
                'sku':i.sku.id,
                'image':i.image,
            })
        return Response({
            'code':200,
            'msg':'获取成功',
            'data':sku_list
        })




    def post(self,request):
        # 接受前端数据
        sku_id = request.data.get('sku')
        image = request.data.get('image')

        if not all([sku_id,image]):
            return Response({
                'code':400,
                'msg':'没有接受到前端数据'
            })
        try:
            SKU.objects.get(id=sku_id)
        except Exception as e:
            print('没有此id》》》',e)
            return Response({
                'code':400,
                'msg':'没有此sku商品'

            })
        try:
            SKUImage.objects.create(sku_id=sku_id,image=image)
            return Response({
                'code':201,
                'msg':'添加成功'
            })
        except Exception as e:
            print('添加失败',e)
            return Response({
                'code':400,
                'msg':'添加失败'
            })

# 编辑和删除
class PicView(APIView):
    def get(self,request,id):
        try:
            skuimage = SKUImage.objects.filter(id=id)
        except Exception as e:
            print('没有此商品图片',e)
            return Response({
                'code':400,
                'msg':'没有此商品图片'
            })

        list1 = []
        for i in skuimage:
            list1.append({
                'id':i.id,
                'sku': i.sku.id,
                'image': i.image,
            })

        return Response({
            'code':200,
            'msg':'成功',
            'data':list1
        })

    # 修改
    def put(self,request,id):
        try:
            skuimage = SKUImage.objects.get(id=id)
        except Exception as e:
            print('没有此商品图片',e)
            return Response({
                'code':400,
                'msg':'没有此商品图片'
            })

        # 接受前端数据
        sku_id = request.data.get('sku')
        image = request.data.get('image')
        print('前端数据图片',sku_id,image)

        if not all([sku_id, image]):
            return Response({
                'code': 400,
                'msg': '没有接受到前端数据'
            })

        if image == 'undefined':
            try:
                skuimage.sku_id = sku_id
                skuimage.save()

                return Response({
                    'code': 200,
                    'msg': '修改成功'
                })
            except Exception as e:
                print('修改失败', e)
                return Response({
                    'code': 400,
                    'msg': '修改失败'
                })
        try:
            skuimage.sku_id = sku_id
            skuimage.image = image
            skuimage.save()

            return Response({
                'code':200,
                'msg':'修改成功'
            })
        except Exception as e:
            print('修改失败',e)
            return Response({
                'code':400,
                'msg':'修改失败'
            })
    # 删除的视图
    def delete(self,request,id):
        try:
            SKUImage.objects.get(id=id).delete()
            return Response({
                'code': 204,
                'msg': '删除成功'
            })
        except Exception as e:
            print('删除失败',e)
            return Response({
                'code':400,
                'msg':'删除失败'
            })























# 获取所有的spu
class SpusssView(APIView):
    def get(self,request):
        # 获取所有数据
        spu = SPU.objects.all()
        ser = SpuSer(spu,many=True)
        return Response(ser.data)


class SpuSpecViews(APIView):
    def get(self,request):
        pecofocaton = SPUSpecification.objects.all()

        ser = PecificationSer(pecofocaton, many=True)

        return Response(ser.data)




# SKU规格
class SKUOptionsView(APIView):
    def get(self,request):
        specif = SKUSpecification.objects.all()

        ser = []

        for i in specif:
            dada = {
                'id':i.id,
                'sku':i.sku_id,
                'spec':i.spec_id,
                'option':i.option_id,
                'sku_name':i.sku.name,
                'spec_name': i.spec.name,
                'option_name': i.option.value,

            }
            ser.append(dada)

        return Response(ser)

    # 添加
    def post(self,request):
        # 接受前端数据
        specsForm = request.data.get('specsForm')
        specsForm1 = request.data.get('specsForm1')
        specsForm2 = request.data.get('specsForm2')


        if not all([specsForm,specsForm1,specsForm2]):
            return Response({
                'code':400,
                'msg':'没有接受到前端数据'
            })

        SKUSpecification.objects.create(
            spec_id=specsForm,
            sku_id=specsForm2,
            option_id=specsForm1,
        )

        return Response({
            'code':201,
            'msg':'添加成功'
        })



# 删除修改

class SkuShanView(APIView):
    def get(self,request,id):
        print('dasasfas达达',id)
        try:
            specif = SKUSpecification.objects.filter(id=id)
        except Exception as e:
            print('dasdfasf',e)
            return Response({
                'code':400,
                'msg':'没有这条数据'
            })

        ser = []

        for i in specif:
            dada = {
                'id': i.id,
                'sku': i.sku_id,
                'spec': i.spec_id,
                'option': i.option_id,
                'sku_name': i.sku.name,
                'spec_name': i.spec.name,
                'option_name': i.option.value,

            }
            ser.append(dada)

        return Response({
            'code':200,
            'msg':'获取成功',
            'data':ser
        })

    # 修改

    def put(self,request,id):
        try:
            specif = SKUSpecification.objects.get(id=id)
        except Exception as e:
            print('dasdfasf',e)
            return Response({
                'code':400,
                'msg':'没有这条数据'
            })

        # 获取前端数据
        specsForm = request.data.get('specsForm')
        specsForm1 = request.data.get('specsForm1')
        specsForm2 = request.data.get('specsForm2')

        if not all([specsForm, specsForm1, specsForm2]):
            return Response({
                'code': 400,
                'msg': '没有接受到前端数据'
            })


        try:
            specif.spec_id=specsForm
            specif.sku_id=specsForm2
            specif.option_id=specsForm1
            specif.save()
            return Response({
                'code':200,
                'msg':'修改成功'
            })
        except Exception as e:
            print('崔欧文',e)
            return Response({
                'code': 201,
                'msg': '修改失败'
            })


    def delete(self,request,id):
        try:
            SKUSpecification.objects.get(id=id).delete()
            return Response({
                'code': 204,
                'msg': '删除成功'
            })
        except Exception as e:
            print('dasdfasf',e)
            return Response({
                'code':400,
                'msg':'没有这条数据'
            })











from goods.models import SKU
from goods.serializers import SKUSer

# SKU所有信息
class SKUsuoyou(APIView):
    def get(self,request):
        sku = SKU.objects.all()

        ser = SKUSer(sku,many=True)

        return Response(ser.data)

class SKUguige(APIView):
    def get(self,request):
        sku = SpecificationOption.objects.all()

        ser = SpecificationOptionSer(sku,many=True)

        return Response(ser.data)














